<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/codeblog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/codeblog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/codeblog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/codeblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/codeblog/css/main.css">


<link rel="stylesheet" href="/codeblog/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/codeblog/lib/pace/pace-theme-minimal.min.css">
  <script src="/codeblog/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"skycoin.gitee.io","root":"/codeblog/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":false,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="[toc] 1.对象1.1 Objectobject 是 es 中最常用的类型之一 创建方式 12345678let person &#x3D; new Object();person.name &#x3D; &quot;jake&quot;;person.age &#x3D; 29;let person &#x3D; &amp;#123;  name: &quot;jake&quot;,  age: 29,&amp;#125;;  虽然属性一般是通过">
<meta property="og:type" content="article">
<meta property="og:title" content="Javascript基础（五）：javascript集合引用类型">
<meta property="og:url" content="http://skycoin.gitee.io/codeblog/2021/02/27/%E9%9B%86%E5%90%88%E5%BC%95%E7%94%A8%E7%B1%BB/index.html">
<meta property="og:site_name" content="若水的前端博客">
<meta property="og:description" content="[toc] 1.对象1.1 Objectobject 是 es 中最常用的类型之一 创建方式 12345678let person &#x3D; new Object();person.name &#x3D; &quot;jake&quot;;person.age &#x3D; 29;let person &#x3D; &amp;#123;  name: &quot;jake&quot;,  age: 29,&amp;#125;;  虽然属性一般是通过">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-02-27T02:14:52.000Z">
<meta property="article:modified_time" content="2021-04-15T01:27:03.115Z">
<meta property="article:author" content="若说">
<meta property="article:tag" content="前端">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://skycoin.gitee.io/codeblog/2021/02/27/%E9%9B%86%E5%90%88%E5%BC%95%E7%94%A8%E7%B1%BB/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Javascript基础（五）：javascript集合引用类型 | 若水的前端博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/codeblog/atom.xml" title="若水的前端博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>
    <a target="_blank" rel="noopener" href="https://your-url" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#70B7FD; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/codeblog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">若水的前端博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">最优秀的模式往往是最简单的东西</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/codeblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/codeblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/codeblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/codeblog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://skycoin.gitee.io/codeblog/2021/02/27/%E9%9B%86%E5%90%88%E5%BC%95%E7%94%A8%E7%B1%BB/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=4083356047,3551369852&fm=26&gp=0.jpg">
      <meta itemprop="name" content="若说">
      <meta itemprop="description" content="立志完成前端知识体系的架构">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="若水的前端博客">
    </span>
      <header class="post-header">
      
        <i class="fa fa-thumb-tack"></i>
        <font color=7D26CD>置顶</font>
      
        <h1 class="post-title" itemprop="name headline">
          Javascript基础（五）：javascript集合引用类型
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-02-27 10:14:52" itemprop="dateCreated datePublished" datetime="2021-02-27T10:14:52+08:00">2021-02-27</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-04-15 09:27:03" itemprop="dateModified" datetime="2021-04-15T09:27:03+08:00">2021-04-15</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/codeblog/categories/%E5%89%8D%E7%AB%AF%E6%A1%86%E6%9E%B6/" itemprop="url" rel="index"><span itemprop="name">前端框架</span></a>
                </span>
            </span>

          
            <span id="/codeblog/2021/02/27/%E9%9B%86%E5%90%88%E5%BC%95%E7%94%A8%E7%B1%BB/" class="post-meta-item leancloud_visitors" data-flag-title="Javascript基础（五）：javascript集合引用类型" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/codeblog/2021/02/27/%E9%9B%86%E5%90%88%E5%BC%95%E7%94%A8%E7%B1%BB/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/codeblog/2021/02/27/%E9%9B%86%E5%90%88%E5%BC%95%E7%94%A8%E7%B1%BB/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>[toc]</p>
<h2 id="1-对象"><a href="#1-对象" class="headerlink" title="1.对象"></a>1.对象</h2><h3 id="1-1-Object"><a href="#1-1-Object" class="headerlink" title="1.1 Object"></a>1.1 Object</h3><p>object 是 es 中最常用的类型之一</p>
<p>创建方式</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> person = <span class="keyword">new</span> <span class="built_in">Object</span>();</span><br><span class="line">person.name = <span class="string">&quot;jake&quot;</span>;</span><br><span class="line">person.age = <span class="number">29</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> person = &#123;</span><br><span class="line">  name: <span class="string">&quot;jake&quot;</span>,</span><br><span class="line">  age: <span class="number">29</span>,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>虽然属性一般是通过点语法来存取的，也可以使用括号</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">person[<span class="string">&#x27;name&#x27;</span>]</span><br><span class="line">person.name</span><br><span class="line"></span><br><span class="line">per = &#123;&#125;</span><br><span class="line">&#123;&#125;</span><br><span class="line">per[<span class="string">&#x27;first name&#x27;</span>] = <span class="string">&#x27;bob&#x27;</span></span><br><span class="line"><span class="string">&quot;bob&quot;</span></span><br><span class="line">per</span><br><span class="line">&#123;first name: <span class="string">&quot;bob&quot;</span>&#125;</span><br><span class="line">per[<span class="string">&#x27;first name&#x27;</span>]</span><br><span class="line"><span class="string">&quot;bob&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="1-2-Array"><a href="#1-2-Array" class="headerlink" title="1.2 Array"></a>1.2 Array</h3><p>es 数组也是一组有序的数据，但跟其他语言不同的是，数组中每个槽位可以存储任意类型的数据。这意味着可以创建一个数组，他的第一个元素是字符串，第二个元素是数值，第三个是对象，es 数组也是动态大小的，会随着数据添加而自动增长</p>
<h4 id="1-2-1-创建数组"><a href="#1-2-1-创建数组" class="headerlink" title="1.2.1 创建数组"></a>1.2.1 创建数组</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = <span class="keyword">new</span> <span class="built_in">Array</span>();</span><br></pre></td></tr></table></figure>

<p>如果知道数组中元素的数量，那么可以给构造函数传入一个值，然后 length 属性就会被自动创建并设置为这个值。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">20</span>);</span><br><span class="line"><span class="keyword">let</span> colors = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>);</span><br></pre></td></tr></table></figure>

<p>在使用 Array 构造函数的时候，也可以省略 new 操作符。结果是一样的</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = <span class="built_in">Array</span>(<span class="number">3</span>);</span><br><span class="line"><span class="keyword">let</span> names = <span class="built_in">Array</span>(<span class="string">&quot;bob&quot;</span>);</span><br></pre></td></tr></table></figure>

<p>另一种创建数组的方法是字面量表示法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>];</span><br><span class="line"><span class="comment">// 与对象一样，在使用数组字面量表示法创建数组不会调用Array构造函数</span></span><br></pre></td></tr></table></figure>

<p>Array 构造函数还有两个 es6 新增的数组静态方法：from 和 of。from()用于将类数组结构转换为数组实例，而 of（）用于将一组参数转换为数组实例</p>
<p>Array.from()的第一个参数是一个类数组对象，即任何可迭代的结构，或者有一个 length 属性和可索引元素的结构。这种方式可用于很多场合</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> m = <span class="keyword">new</span> <span class="built_in">Map</span>().set(<span class="number">1</span>, <span class="number">2</span>);</span><br><span class="line"><span class="built_in">Array</span>.from(m); <span class="comment">// [[1,2]]</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getArray</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">Array</span>.from(<span class="built_in">arguments</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">Array</span>.from(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>);</span><br></pre></td></tr></table></figure>

<p>Array.from()还接收第二个可选的映射函数。这个函数可以直接增强新数组的值，而不许像调用 Array.from().map()那样先创建一个中间数组，还可以接收第三个参数，用于指定映射函数中的 this 值。但这个重写的 this 在箭头函数中不适用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const a1 &#x3D; [1,2,3,4]</span><br><span class="line">const a2 &#x3D; Array.from(a1,x &#x3D;&gt; x**2)</span><br><span class="line">const a3 &#x3D; Array.from(a1,function(x)&#123;</span><br><span class="line">	return x ** this.expoenent</span><br><span class="line">&#125;,&#123;expoenent:2&#125;)</span><br></pre></td></tr></table></figure>

<p>Array.of()可以把一组参数转换为数组。这个方法用于替代在 es6 之前常用的 Array.prototype.slice.call(arguments)，一种异常笨拙的将 arguments 对象转换成为数组的写法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Array</span>.of(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>); <span class="comment">// [1,2,3,4]</span></span><br><span class="line"><span class="built_in">Array</span>.of(<span class="literal">undefined</span>); <span class="comment">// [undefined]</span></span><br></pre></td></tr></table></figure>

<h4 id="1-2-2-数组空位"><a href="#1-2-2-数组空位" class="headerlink" title="1.2.2 数组空位"></a>1.2.2 数组空位</h4><p>使用数组字面量初始化数组时，可以使用一串逗号来创建空位(hole)</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> options = [, , , , ,]; <span class="comment">//创建包含5个元素的数组</span></span><br><span class="line"><span class="built_in">console</span>.log(options.length); <span class="comment">// 5</span></span><br></pre></td></tr></table></figure>

<p>es6 新增方法和迭代器与早期 es 版本中存在的方法行为不同。ES6 新增方法普遍将这些空位当成存在的元素，只不过值是 undefined</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> options = [<span class="number">1</span>，，，，<span class="number">5</span>]</span><br><span class="line">options.map(<span class="function">() =&gt;</span> <span class="number">6</span>) <span class="comment">// [6,,,,,6]</span></span><br></pre></td></tr></table></figure>

<h4 id="1-2-3-数组索引"><a href="#1-2-3-数组索引" class="headerlink" title="1.2.3 数组索引"></a>1.2.3 数组索引</h4><p>要取得或设置数组的值，需要使用中括号并提供相应值的数字索引</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 数组length属性的独到之处是，他不是只读的。通过修改length属性，可以从数组末尾删除或添加元素</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> colors = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>];</span><br><span class="line">colors.length = <span class="number">2</span>;</span><br><span class="line">colors[<span class="number">2</span>]; <span class="comment">// undefined</span></span><br><span class="line"><span class="comment">// 可以从数组末尾删除或添加元素</span></span><br></pre></td></tr></table></figure>

<h4 id="1-2-4-检测数组"><a href="#1-2-4-检测数组" class="headerlink" title="1.2.4 检测数组"></a>1.2.4 检测数组</h4><p>判断一个对象是不是数组</p>
<p>只有一个网页（因而只有一个全局作用域）的情况下，使用 instanceof 操作</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(value <span class="keyword">instanceof</span> <span class="built_in">Array</span>)&#123;</span><br><span class="line">  <span class="comment">// 操作数组</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="1-2-5-迭代器方法"><a href="#1-2-5-迭代器方法" class="headerlink" title="1.2.5 迭代器方法"></a>1.2.5 迭代器方法</h4><p>在 es6 中，Array 的原型上暴露 3 个用于检索数组内容的方法：keys()、values()和 entries()。keys()返回数组索引的迭代器，values（）返回数组元素的迭代器，而 entries()返回索引/值对的迭代器</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> a = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>];</span><br><span class="line"><span class="keyword">const</span> akeys = <span class="built_in">Array</span>.from(a.keys());</span><br><span class="line"><span class="keyword">const</span> aValues = <span class="built_in">Array</span>.from(a.values());</span><br><span class="line"><span class="keyword">const</span> aEntries = <span class="built_in">Array</span>.from(a.entries);</span><br></pre></td></tr></table></figure>

<p>使用 es6 的解构可以非常容易的循环</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> a = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>];</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> [idx, ele] <span class="keyword">of</span> a.entries()) &#123;</span><br><span class="line">  alert(idx);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="1-2-6-复制和填充方法"><a href="#1-2-6-复制和填充方法" class="headerlink" title="1.2.6 复制和填充方法"></a>1.2.6 复制和填充方法</h4><p>es6 新增了两个方法：批量复制方法 copyWithin()，以及填充数组方法 fill()。这两个方法的函数签名类似，都需要指定既有数组实例上的一个范围，包括开始的索引，不包含结束索引，使用方法不会改变数组的大小</p>
<p>使用 fill()的方法可以向一个已有的数组中插入全部或部分相同的值。开始索引用于指定开始填充的位置，它是可选的</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> zeros = [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>];</span><br><span class="line">zeros.fill(<span class="number">5</span>);</span><br><span class="line">zeros.fill(<span class="number">0</span>);</span><br><span class="line">zeros.fill(<span class="number">6</span>, <span class="number">3</span>); <span class="comment">// [0,0,0,6,6]</span></span><br><span class="line">zeros.fill(<span class="number">0</span>); <span class="comment">// 重置</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 填充索引大于等于1 且小于3的元素</span></span><br><span class="line">zeros.fill(<span class="number">7</span>, <span class="number">1</span>, <span class="number">3</span>); <span class="comment">// [0,7,7,0,0]</span></span><br><span class="line"></span><br><span class="line">zeros.fill(<span class="number">8</span>, -<span class="number">4</span>, -<span class="number">1</span>); <span class="comment">// [0,8,8,8,0]</span></span><br></pre></td></tr></table></figure>

<p>Fill()静默忽略超出数组边界、零长度及方向相反的索引范围</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> zeroes = [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>];</span><br><span class="line">zeros.fill(<span class="number">1</span>, -<span class="number">10</span>, -<span class="number">6</span>); <span class="comment">// [0,0,0,0,0,0]</span></span><br><span class="line"><span class="comment">// 索引过高，忽略</span></span><br><span class="line">zeros.fill(<span class="number">1</span>, <span class="number">10</span>, <span class="number">15</span>); <span class="comment">// 0,0,0,0,0</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 索引过高忽略</span></span><br><span class="line">zeros.fill(<span class="number">4</span>, <span class="number">3</span>, <span class="number">10</span>); <span class="comment">//[0,0,0,0,0]</span></span><br></pre></td></tr></table></figure>

<p>与 fill 不同 copyWithin()会按照指定范围浅复制数组中的部分内容，然后将他们插入到指定索引开始的位置。开始索引和结束索引则 fill()使用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> ints,</span><br><span class="line">  reset = <span class="function">() =&gt;</span> (ints = [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]);</span><br><span class="line">reset();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 复制数组的前面两个元素到后面两个元素</span></span><br><span class="line"><span class="comment">// copyWithin() 方法用于从数组的指定位置拷贝元素到数组的另一个指定位置中。</span></span><br><span class="line"><span class="keyword">var</span> fruits = [<span class="string">&quot;Banana&quot;</span>, <span class="string">&quot;Orange&quot;</span>, <span class="string">&quot;Apple&quot;</span>, <span class="string">&quot;Mango&quot;</span>];</span><br><span class="line">fruits.copyWithin(<span class="number">2</span>, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// copyWithin()j静默忽略超出数组边界、零长度及方向相反的索引范围</span></span><br><span class="line"><span class="keyword">let</span> ints;</span><br><span class="line">reset = <span class="function">() =&gt;</span> (ints = [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]);</span><br><span class="line">reset();</span><br><span class="line"><span class="comment">// 索引过低忽略</span></span><br></pre></td></tr></table></figure>

<h4 id="1-2-7-转换方法"><a href="#1-2-7-转换方法" class="headerlink" title="1.2.7 转换方法"></a>1.2.7 转换方法</h4><p>所有对象都有 toLocaleString()、toString()和 valueOf（）方法。其中 valueOf（）返回的还是数组本身。而 toString()返回由数组中每个值的等效字符串拼接而成的一个一个逗号分隔的字符串。也就是说每个值都会调用 toString 方法，以得到最终的字符串</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>];</span><br><span class="line">colors</span><br><span class="line">  .toString() <span class="comment">// a,b,c</span></span><br><span class="line"></span><br><span class="line">  [(<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>)].valueOf()(<span class="number">3</span>)</span><br><span class="line">  [(<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>)][(<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>)].toString();</span><br><span class="line"><span class="string">&quot;a,b,c&quot;</span>[(<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>)].toLocaleString();</span><br><span class="line">(<span class="string">&quot;a,b,c&quot;</span>);</span><br></pre></td></tr></table></figure>

<p>Join 方法</p>
<p>如果数组中某一项是 null 或 undefined,则在，则在 join()、toLocaleString()、toString()和 valueOf 返回的结果中会以空字符串表示</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>];</span><br><span class="line">colors.join(<span class="string">&quot;,&quot;</span>);</span><br><span class="line">colors.join(<span class="string">&quot;||&quot;</span>);</span><br></pre></td></tr></table></figure>

<h4 id="1-2-8-栈方法"><a href="#1-2-8-栈方法" class="headerlink" title="1.2.8 栈方法"></a>1.2.8 栈方法</h4><p>es 给数组提供几个方法，让他看起来像是另外一种数据结构。数组对象可以像栈一样，也就是一种限制插入和删除项的数据结构。</p>
<p>Push()方法接受任意数量的参数，并将它们添加到数组末尾，返回数组的最新长度。pop()方法则用于删除数组的最后一项，同时减少数组的 length 值，返回被删除的项</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = <span class="keyword">new</span> <span class="built_in">Array</span>();</span><br><span class="line"><span class="keyword">let</span> count = colors.push(<span class="string">&quot;red&quot;</span>, <span class="string">&quot;green&quot;</span>);</span><br><span class="line">alert(count);</span><br><span class="line">count = colors.push(<span class="string">&quot;black&quot;</span>);</span><br><span class="line">alert(count);</span><br><span class="line"><span class="keyword">let</span> item = colors.pop();</span><br><span class="line">alert(item);</span><br><span class="line">alert(colors.length);</span><br></pre></td></tr></table></figure>

<h4 id="1-2-9-队列方法"><a href="#1-2-9-队列方法" class="headerlink" title="1.2.9 队列方法"></a>1.2.9 队列方法</h4><p>队列以先进先出形式进行访问。要想要模拟队列就差一个从数组开头取得数据的方法了。这个数组方法叫做 shift(),它会删除数组的第一项并返回它，然后数组长度减 1.使用 shift()和 push()</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = <span class="keyword">new</span> <span class="built_in">Array</span>();</span><br><span class="line"><span class="keyword">let</span> count = colors.push(<span class="string">&quot;red&quot;</span>, <span class="string">&quot;green&quot;</span>);</span><br><span class="line">count = colors.push(<span class="string">&quot;black&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> item = color.shift(); <span class="comment">// 取得第一项</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// es也为数组提供了unshift()方法。</span></span><br><span class="line"><span class="comment">// 在数组开头添加任意多个值，然后返回新的数组长度。</span></span><br></pre></td></tr></table></figure>

<h4 id="1-2-10-排序方法"><a href="#1-2-10-排序方法" class="headerlink" title="1.2.10 排序方法"></a>1.2.10 排序方法</h4><p>两个方法可以用来排序 ： reverse()和 sort()</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> values = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>];</span><br><span class="line">values.reverse(); <span class="comment">// [4, 3, 2, 1]</span></span><br></pre></td></tr></table></figure>

<p>默认情况下使用 sort()会按照升序重新排列数组元素。</p>
<p>为此 sort（）会在每一项上调用 String()转型函数，然后比较字符串来决定顺序。即使数组的元素都是数值，也会先把数组转换为字符</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> values = [<span class="number">0</span>, <span class="number">1</span>, <span class="number">10</span>, <span class="number">5</span>];</span><br><span class="line">values.sort(); <span class="comment">// [0, 1, 10, 5]</span></span><br></pre></td></tr></table></figure>

<p>因为字符串“10”在字符串”5”的前头。为此 sort()方法可以接受一个比较函数，用于判断哪个值应该排在前面</p>
<p>比较函数接受的两个参数，如果第一个参数应该排到第二个参数前面，就会返回负值；两个参数相等，返回 0；如果第一个参数应该排在第二个参数后面，就返回正值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">compare</span>(<span class="params">value1, value2</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (value1 &lt; value2) &#123;</span><br><span class="line">    <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span> (value1 &gt; value2) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这个比较函数可以适用于大多数数据类型，可以把它当成参数传给 sort（）方法，如下</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> values = [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">3</span>];</span><br><span class="line">values.sort(compare); <span class="comment">// [0,1,2,3,4]</span></span><br></pre></td></tr></table></figure>

<p>如果数组的元素是数值，或者是其 valueOf()方法返回数值的对象</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">compare</span>(<span class="params">value1, value2</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> value2 - value1;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> points = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">30</span>, <span class="number">4</span>, <span class="number">3</span>];</span><br><span class="line">points.sort(<span class="function">(<span class="params">a, b</span>) =&gt;</span> a - b); <span class="comment">// (5) [1, 2, 3, 4, 30]</span></span><br></pre></td></tr></table></figure>

<p><strong>补充 sort 原理</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">如果调用该方法时没有使用参数，将按字母顺序对数组中的元素进行排序，说得更精确点，是按照字符编码的顺序进行排序。要实现这一点，首先应把数组的元素都转换成字符串（如有必要），以便进行比较。</span><br><span class="line"></span><br><span class="line">如果想按照其他标准进行排序，就需要提供比较函数，该函数要比较两个值，然后返回一个用于说明这两个值的相对顺序的数字。比较函数应该具有两个参数 a 和 b，其返回值如下：</span><br><span class="line"></span><br><span class="line">若 a 小于 b，在排序后的数组中 a 应该出现在 b 之前，则返回一个小于 <span class="number">0</span> 的值。</span><br><span class="line">若 a 等于 b，则返回 <span class="number">0</span>。</span><br><span class="line">若 a 大于 b，则返回一个大于 <span class="number">0</span> 的值。</span><br></pre></td></tr></table></figure>

<h4 id="1-2-11-操作方法"><a href="#1-2-11-操作方法" class="headerlink" title="1.2.11 操作方法"></a>1.2.11 操作方法</h4><p>对于数组中的元素，我们有很多操作方法。</p>
<ul>
<li>Concat（）方法可以在现有数组的全部元素基础上创建一个新数组。它首先会创建一个当前数组的副本，然后把它的参数添加到副本末尾，最后返回这个新构建的数组。如果传入一个数组，则 concat()会把这些数组的每一项都添加到结果数组。如果参数不是数组，则直接把它们添加到结果数组末尾。</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = [<span class="string">&quot;red&quot;</span>, <span class="string">&quot;green&quot;</span>, <span class="string">&quot;blue&quot;</span>];</span><br><span class="line"><span class="keyword">let</span> colors2 = colors.concat(<span class="string">&quot;yellow&quot;</span>, [<span class="string">&quot;black&quot;</span>, <span class="string">&quot;brow&quot;</span>]); <span class="comment">// [&#x27;red&#x27;&#x27;&#x27;,&#x27;green&#x27;,&#x27;blue&#x27;,&#x27;yellow&#x27;,&#x27;black&#x27;,&#x27;brow&#x27;]</span></span><br></pre></td></tr></table></figure>

<p>打平数组参数的行为可以重写，方法实在参数数组上指定一个特殊的符号：Symbol.isConcat-Spreadable。这个符号能够阻止 concat()打平参数数组。相反，把这个值设置为 true 可以强制打平类数组对象</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>];</span><br><span class="line"><span class="keyword">let</span> newColors = [<span class="string">&quot;d&quot;</span>, <span class="string">&quot;e&quot;</span>];</span><br><span class="line"><span class="keyword">let</span> more = &#123;</span><br><span class="line">  [<span class="built_in">Symbol</span>.isConcatSpreadable]: <span class="literal">true</span>,</span><br><span class="line">  length: <span class="number">2</span>,</span><br><span class="line">  <span class="number">0</span>: <span class="string">&quot;a&quot;</span>,</span><br><span class="line">  <span class="number">1</span>: <span class="string">&quot;b&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line">newColors[<span class="built_in">Symbol</span>.isConcatSpreadable] = <span class="literal">false</span>;</span><br><span class="line"><span class="comment">// 强制不打平数组</span></span><br><span class="line"><span class="keyword">let</span> colors2 = colors.concat(<span class="string">&quot;yellow&quot;</span>, newColors);</span><br></pre></td></tr></table></figure>

<p>方法 slice()用于创建一个包含原有数组中一个或多个元素的新数组。slice()方法可以接受一个或两个参数：返回元素的开始索引和结束索引。如果只有一个参数，则 slice()会返回该索引到数组末尾的所有元素。如果有两个元素，slice()返回从开始索引到结束索引对应的所有元素，其中不包含结束索引对应的元素</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>];</span><br><span class="line"><span class="keyword">let</span> a = colors.slice(<span class="number">1</span>); <span class="comment">// b,c,d</span></span><br><span class="line"><span class="keyword">let</span> b = colors.slice(<span class="number">1</span>, <span class="number">4</span>); <span class="comment">// b,c,d</span></span><br></pre></td></tr></table></figure>

<p>如果 slice()的参数有负值，那么就以数值长度加上这个负值的结果确定位置</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>];</span><br><span class="line">a.slice(-<span class="number">1</span>); <span class="comment">// d</span></span><br></pre></td></tr></table></figure>

<p>或许最强大的数组方法就属 splice()了，使用它的方式就可以有很多种。splice（）主要目的是在数组中间插入元素，但是 3 种不同的方式使用这个方法</p>
<ul>
<li>删除，需要给 splice()传两个参数：要删除的第一个元素的位置和删除元素的数量</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">b = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>](<span class="number">4</span>)[(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>)];</span><br><span class="line">b.splice(<span class="number">1</span>, <span class="number">2</span>)(<span class="number">2</span>)[(<span class="number">2</span>, <span class="number">3</span>)];</span><br></pre></td></tr></table></figure>

<ul>
<li>插入。需要给 splice()传 3 个参数：开始位置、0（要删除的元素数量）和要插入的元素</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">b</span><br><span class="line">(<span class="number">2</span>) [<span class="number">1</span>, <span class="number">4</span>]</span><br><span class="line">b.splice(<span class="number">0</span>,<span class="number">0</span>,[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>])</span><br><span class="line">[]</span><br><span class="line">b</span><br><span class="line">(<span class="number">3</span>) [<span class="built_in">Array</span>(<span class="number">3</span>), <span class="number">1</span>, <span class="number">4</span>]</span><br><span class="line">b.splice(<span class="number">0</span>,<span class="number">0</span>,<span class="number">1</span>)</span><br><span class="line">[]</span><br><span class="line">b</span><br><span class="line">(<span class="number">4</span>) [<span class="number">1</span>, <span class="built_in">Array</span>(<span class="number">3</span>), <span class="number">1</span>, <span class="number">4</span>]</span><br></pre></td></tr></table></figure>

<ul>
<li>替换。splice()在删除元素的同时可以在指定位置插入新元素，同样需要传递 3 个参数：开始位置、要删除元素的数量和要插入的任意多个元素。要插入的元素数量不一定跟删除的元素数量一致</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">b(<span class="number">4</span>)[(<span class="number">1</span>, <span class="built_in">Array</span>(<span class="number">3</span>), <span class="number">1</span>, <span class="number">4</span>)];</span><br><span class="line"></span><br><span class="line">b.splice(<span class="number">2</span>, <span class="number">1</span>, <span class="string">&quot;red&quot;</span>, <span class="string">&quot;green&quot;</span>)[<span class="number">1</span>];</span><br><span class="line">b(<span class="number">5</span>)[(<span class="number">1</span>, <span class="built_in">Array</span>(<span class="number">3</span>), <span class="string">&quot;red&quot;</span>, <span class="string">&quot;green&quot;</span>, <span class="number">4</span>)];</span><br></pre></td></tr></table></figure>

<p>Splice()方法始终返回这样一个数组，它包含从数组中被删除的元素（如果没有删除元素，则返回空数组）。</p>
<h4 id="1-2-12-搜索和位置方法"><a href="#1-2-12-搜索和位置方法" class="headerlink" title="1.2.12 搜索和位置方法"></a>1.2.12 搜索和位置方法</h4><p>es 提供两类搜索数组的方法：按照严格相等搜索和按断言函数搜索</p>
<p><strong>1.严格相等</strong></p>
<p>es 提供了三个严格相等的搜索方法：indexOf()、lastIndexOf()和 Includes()。其中，前两个方法在所有版本中都可用，第三个是 es7。这些方法都接收两个参数：要查找的元素和一个可选的起始搜索位置。indexOf()和 includes（）方法从数组前头开始往后搜索，lastIndexOf 是从后往前</p>
<p>indexOf 和 lastIndexOf()都返回要查找的元素在数组中的位置，如果没有找到则返回-1.includes（）返回布尔值，表示是否至少找到一个与指定元素匹配的项。会使用全等进行比较。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> number = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line">number.indexOf(<span class="number">4</span>); <span class="comment">// 5 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。</span></span><br><span class="line"><span class="comment">// 第二个参数代表从哪个元素进行搜索</span></span><br></pre></td></tr></table></figure>

<p><strong>2.断言函数</strong></p>
<p>es 也允许按照定义的断言函数搜索数组，每个索引都会调用这个函数，断言函数的返回值决定了相应索引是否被认为匹配</p>
<p>断言函数接受 3 个参数：元素、索引和数组本身。其中元素是数组当前搜索的元素，索引是当前元素的索引，而数组就是正在搜索的数组。断言函数返回真值，表示是否匹配</p>
<p>find()和 findIndex（）方法使用了断言函数。这两个方法都从数组的最小索引开始。find()返回第一个匹配的元素，findIndex()返回第一个匹配元素的索引。这两个方法也都接收第二个可选的参数，用于指定断言函数内部的 this 值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> people = [</span><br><span class="line">  &#123;</span><br><span class="line">    name: <span class="string">&quot;mat&quot;</span>,</span><br><span class="line">    age: <span class="number">27</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    name: <span class="string">&quot;nicole&quot;</span>,</span><br><span class="line">    age: <span class="number">29</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">];</span><br><span class="line">people.find(<span class="function">(<span class="params">element, idx, array</span>) =&gt;</span> element.age &lt; <span class="number">28</span>);</span><br></pre></td></tr></table></figure>

<p>找到匹配后，这两个方法都不继续搜索</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> evens = [<span class="number">2</span>,<span class="number">4</span>,<span class="number">6</span>]</span><br><span class="line">evens.find(<span class="function">(<span class="params">ele,idx,array</span>) =&gt;</span> &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">[<span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;3&#x27;</span>].map(<span class="built_in">parseInt</span>)</span><br></pre></td></tr></table></figure>

<h4 id="1-2-13-迭代方法"><a href="#1-2-13-迭代方法" class="headerlink" title="1.2.13 迭代方法"></a>1.2.13 迭代方法</h4><p>es 为数组定义了 5 个迭代方法。每个方法接收两个参数：以每一项为参数运行的函数，以及可选的作为函数作用域对象（影响函数中 this 的值）。传给每个方法的函数接受 3 个参数：数组元素、元素索引和数组本身。因具体方法而异，这个函数的执行结果可能会也可能不会影响返回值</p>
<ul>
<li>Every：对数组每一项都运行传入的函数，如果对每一项函数都返回 true，则这个方法返回 true</li>
<li>Filter:为数组每一项都运行传入的函数，函数返回 true 的项会组成数组之后返回</li>
<li>forEach：对数组每一项运行传入的函数，没有返回值</li>
<li>Map:对数组每一项都运行传入的函数，返回由每次函数调用的结果构成的数组</li>
<li>Some:对数组每一项都运行传入的函数，如果有一项函数返回 true，则这个方法返回 true</li>
</ul>
<p>这些方法都不改变调用他们的数组</p>
<p>every 和 some 是最相似的，都是从数组中搜索复合某个条件的元素。对于 every 来说，传入的函数必须对每一项都返回 true，它才会返回 true，否则返回 false。对 some 来说，只要有一项让传入的函数返回 true，他就回返回 true</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> number = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>];</span><br><span class="line"><span class="keyword">let</span> everyRes = number.every(<span class="function">(<span class="params">item, index, array</span>) =&gt;</span> item &gt; <span class="number">2</span>);</span><br><span class="line"><span class="keyword">let</span> someRes = number.some(<span class="function">(<span class="params">item, index, array</span>) =&gt;</span> item &gt; <span class="number">2</span>);</span><br></pre></td></tr></table></figure>

<p>filter 是基于给定的函数来决定某一项是否应该包含在它的返回的数组中。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> number = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, , <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">let</span> filterRes = number.filter(<span class="function">(<span class="params">item, index, array</span>) =&gt;</span> item &gt; <span class="number">2</span>);</span><br></pre></td></tr></table></figure>

<p>map 的方法也会返回一个数组，这个数组的每一项都是对原始数组中同样位置的元素运行传入函数而返回的结果。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> number = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, , <span class="number">5</span>];</span><br><span class="line"><span class="keyword">let</span> mapRes = number.map(<span class="function">(<span class="params">item, index, array</span>) =&gt;</span> item * <span class="number">2</span>);</span><br></pre></td></tr></table></figure>

<p>forEach 方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> number = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line">number.forEach(<span class="function">(<span class="params">item,index,array</span>) =&gt;</span> &#123;<span class="comment">//执行操作&#125;)</span></span><br></pre></td></tr></table></figure>

<p><strong>补充</strong> map 的知识用法</p>
<ul>
<li>map 仅对已分配值（包括）的数组索引进行 callback 调用。</li>
</ul>
<p>题目：[‘1’,’2’,’3’].map(parseInt)</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">答案：[<span class="number">1</span>,<span class="literal">NaN</span>,<span class="literal">NaN</span>]</span><br><span class="line">语法： array.map(callback(currentValue,index,arr),thisValue)</span><br><span class="line">callback:为arr的每个元素调用的函数。每次执行callback时，返回值都会被添加到newArray中。</span><br></pre></td></tr></table></figure>

<ul>
<li>Map()方法返回一个新数组，数组中的元素为原始数组元素调用函数处理后的值</li>
<li>map()方法按照原始数组顺序依次处理元素</li>
<li>map()不会对空数组进行检测</li>
<li>Map()不会改变原始数组</li>
<li>返回值：返回一个新数组，数组中的元素为原始数组元素调用函数处理后的值</li>
<li>thisValue：可选，对象作为该执行回调时使用，传递给函数，用作 this 的值。如果省略了 thisValu,或者传入 null、undefined,那么回调函数的 this 为全局对象</li>
</ul>
<p>回顾 parseInt 运算符</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">parseInt</span>(string, radix)</span><br><span class="line">string:需要解析的字符串</span><br><span class="line">radix:需要解析数字的基数，该值介于<span class="number">2</span>-<span class="number">36</span>之间</span><br><span class="line">如果省略该参数，或者该参数值为<span class="number">0</span>，则数字将以<span class="number">10</span>为基础进行解析。</span><br><span class="line">如果该参数小于<span class="number">2</span>或者大于<span class="number">36</span>，则<span class="built_in">parseInt</span>()将返回<span class="literal">NaN</span></span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">类型转换之显式转换</span><br><span class="line">了解这两个函数后，我们可以模拟一下运行情况</span><br><span class="line"><span class="built_in">parseInt</span>(<span class="string">&#x27;1&#x27;</span>, <span class="number">0</span>) <span class="comment">//radix为0时，且string参数不以&#x27;0x&#x27;和&#x27;0&#x27;开头时，按照10为基数处理，这个时候返回1</span></span><br><span class="line"><span class="built_in">parseInt</span>(<span class="string">&#x27;2&#x27;</span>, <span class="number">1</span>) <span class="comment">//基数为1（1进制）表示的数中，最大值小于2，所以无法解析，返回NaN</span></span><br><span class="line"><span class="built_in">parseInt</span>(<span class="string">&#x27;3&#x27;</span>, <span class="number">2</span>) <span class="comment">//基数为2（2进制）表示的数中，最大值小于3，所以无法解析，返回NaN</span></span><br></pre></td></tr></table></figure>

<h4 id="1-2-14-归并方法"><a href="#1-2-14-归并方法" class="headerlink" title="1.2.14 归并方法"></a>1.2.14 归并方法</h4><p>es 为数组提供了两个归并方法：reduce 和 reduceRight。这两个方法都会迭代数组的所有项，并在此基础上构建一个最终返回值。</p>
<p>这两个方法都会接收两个参数：对每一项都会运行的归并函数，以及可选的以之为归并起点的初始值。传给的参数：上一个归并值、当前项、当前项的索引和数组本身。这个函数返回的任何值都会作为下一次调用同一个函数。如果没有给这两个方法传入可选的第二个参数，则第一次迭代从数组的第二项开始，因此传给归并函数的第一个参数是数组的第一项，第二个参数是数组的第二项</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&gt; <span class="keyword">let</span> v = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line">&gt; <span class="literal">undefined</span></span><br><span class="line">&gt; v.reduce(<span class="function">(<span class="params">prev,cur,index,array</span>) =&gt;</span> prev + cur)</span><br><span class="line">&gt; <span class="number">10</span></span><br><span class="line"></span><br><span class="line">&gt; <span class="keyword">let</span> m = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">4</span>,<span class="number">4</span>]</span><br><span class="line">&gt; <span class="literal">undefined</span></span><br><span class="line">&gt; m.reduceRight(<span class="function"><span class="keyword">function</span>(<span class="params">prev,cur,index,array</span>)</span>&#123;<span class="keyword">return</span> prev + cur&#125;)</span><br><span class="line">&gt; <span class="number">11</span></span><br></pre></td></tr></table></figure>

<h3 id="1-3-定型数组"><a href="#1-3-定型数组" class="headerlink" title="1.3 定型数组"></a>1.3 定型数组</h3><p>定型数组（typed array)是 es 新增的结构，目的是为了提升向原生库传输数据的效率。js 并没有 typedArray 类型，它所指的其实是一种特殊包含数值类型的数组。</p>
<h4 id="1-3-1-历史"><a href="#1-3-1-历史" class="headerlink" title="1.3.1 历史"></a>1.3.1 历史</h4><p>随着浏览器流行，人们希望它来运行复杂的 3D 应用程序。</p>
<ul>
<li>WebGl</li>
</ul>
<p>是一种 3D 绘图协议，这种绘图技术标准允许把 JavaScript 和 OpenGL ES 2.0 结合在一起，通过增加 OpenGL ES 2.0 的一个 JavaScript 绑定，WebGL 可以为 HTML5 Canvas 提供硬件 3D 加速渲染，这样 Web 开发人员就可以借助系统显卡来在浏览器里更流畅地展示 3D 场景和模型了，还能创建复杂的导航和<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E6%95%B0%E6%8D%AE%E8%A7%86%E8%A7%89%E5%8C%96/9900523">数据视觉化</a>。显然，WebGL 技术标准免去了开发网页专用渲染插件的麻烦，可被用于创建具有复杂 3D 结构的网站页面，甚至可以用来设计 3D 网页游戏等等</p>
<ul>
<li>定型数组</li>
</ul>
<p>这是一个提供 js 接口的、c 语言风格的浮点数组。js 运行时使用这个类型可以分配、读取和写入数组。这个数组可以直接传给底层图形驱动程序 API，也可以直接从底层获取到</p>
<h4 id="1-3-2-ArrayBuffer"><a href="#1-3-2-ArrayBuffer" class="headerlink" title="1.3.2 ArrayBuffer"></a>1.3.2 ArrayBuffer</h4><p>Float32Array 实际上是一种视图，可以允许 js 运行时访问一块名为 ArrayBuffer 的预分配内存。ArrayBuffer 是所有定型数组及视图引用的基本单位</p>
<p>ArrayBuffer 是一个普通的构造函数，可用于在内存中分配特定数量的字节空间</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> buf = <span class="keyword">new</span> <span class="built_in">ArrayBuffer</span>(<span class="number">16</span>); <span class="comment">// 在内存中分配16字节</span></span><br><span class="line">buf.byteLength;</span><br></pre></td></tr></table></figure>

<p>一经创建就不能在调整大小。不过，可以使用 slice()复制其全部或部分到一个实例中</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> buf1 = <span class="keyword">new</span> <span class="built_in">ArrayBuffer</span>(<span class="number">16</span>)</span><br><span class="line"><span class="keyword">const</span> buf2 = buf1,slice(<span class="number">4</span>,<span class="number">12</span>)</span><br><span class="line">buf2.byteLength. <span class="comment">// 8</span></span><br></pre></td></tr></table></figure>

<p>ArrayBuffer 类似于 c++中的 malloc,但也有几个区别</p>
<ul>
<li>malloc 在分配失败会返回一个 null 指针，ArrayBuffer 在分配失败时会抛出失败</li>
<li>malloc 可以利用虚拟内存，因此最大可分配尺寸可寻址系统内存限制</li>
<li>malloc 调用成功不会初始化实际的地址。声明 ArrayBuffer 则会将所有二进制位初始化为 0</li>
<li>通过 malloc 分配的堆内存除非调用 free 或程序退出，否则系统不能在使用。而通过声明 ArrayBuffer 分配的堆内存可以被当成垃圾回收，不用手动释放</li>
</ul>
<p>应用场景：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">大量的 Web API 用到了<span class="built_in">ArrayBuffer</span>对象和它的视图对象。</span><br><span class="line"></span><br><span class="line">ajax</span><br><span class="line">传统上，服务器通过 AJAX 操作只能返回文本数据，即responseType属性默认为text。XMLHttpRequest第二版XHR2允许服务器返回二进制数据，这时分成两种情况。如果明确知道返回的二进制数据类型，可以把返回类型（responseType）设为arraybuffer；如果不知道，就设为blob。</span><br></pre></td></tr></table></figure>

<h4 id="1-3-3-DateView"><a href="#1-3-3-DateView" class="headerlink" title="1.3.3 DateView"></a>1.3.3 DateView</h4><p>第一种允许你读写 ArrayBuffer 的视图是 DateView.这个视图专为文件 i/o 和网络 i/o 设计</p>
<h4 id="1-3-4-定型数组"><a href="#1-3-4-定型数组" class="headerlink" title="1.3.4 定型数组"></a>1.3.4 定型数组</h4><p>视图：视图是指计算机数据库中的视图，是一个虚拟表，其内容由查询定义。同真实的表一样，视图包含一系列带有名称的列和行数据。</p>
<p>另一种形式的 ArrayBuffer 视图，它特定于一种 elementType 且遵循系统原生的字节序</p>
<p>定型数组行为，从很多数组与普通数组都很相似。定型数组支持如下操作符、方法和属性</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">[];</span><br><span class="line">copyWithin;</span><br><span class="line">entries;</span><br><span class="line">every;</span><br><span class="line">fill;</span><br><span class="line">filter;</span><br><span class="line">find;</span><br><span class="line">findIndex;</span><br><span class="line">forEach;</span><br><span class="line">indexOf;</span><br><span class="line">join;</span><br><span class="line">keys;</span><br><span class="line">lastIndexOf;</span><br><span class="line">length;</span><br><span class="line">map;</span><br><span class="line">reduce;</span><br><span class="line">reduceRight;</span><br><span class="line">reverse;</span><br><span class="line">slice;</span><br><span class="line">some;</span><br><span class="line">sort;</span><br><span class="line">toLocaleString;</span><br><span class="line">toString;</span><br><span class="line">values;</span><br></pre></td></tr></table></figure>

<p>其中返回新数组方法也会返回包含相同元素类型的新定型数组</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> ints = <span class="keyword">new</span> <span class="built_in">Int16Array</span>([<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>]);</span><br><span class="line"><span class="keyword">const</span> doubles = ints.map(<span class="function">(<span class="params">x</span>) =&gt;</span> <span class="number">2</span> * x);</span><br><span class="line">doubles <span class="keyword">instanceof</span> <span class="built_in">Int16Array</span>; <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<p>定型数组有一个 Symbol.iterator 符号属性，因此可以通过 for…of 循环和扩展运算符来操作</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> ints = <span class="keyword">new</span> <span class="built_in">Int16Array</span>([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> int <span class="keyword">of</span> ints) &#123;</span><br><span class="line">  alert(int);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>合并、复制和修改定型数组</li>
</ul>
<p>定型数组同样适用数组缓冲来存储数据，而数组缓冲无法调整大小。因此，下列方法不适用定型数组</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">concat();</span><br><span class="line">pop();</span><br><span class="line">push();</span><br><span class="line">shift();</span><br><span class="line">splice();</span><br><span class="line">unshift();</span><br></pre></td></tr></table></figure>

<p>不过，定型数组也提供了两个新方法，可以快速向外或向内复制数据：set()和 subarray()</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">subarray() 返回一个新的、基于相同 <span class="built_in">ArrayBuffer</span>、元素类型也相同的的 _TypedArray_。开始的索引将会被包括，而结束的索引将不会被包括。TypedArray 是指 typed array types 的其中之一。</span><br></pre></td></tr></table></figure>

<h3 id="1-4-Map"><a href="#1-4-Map" class="headerlink" title="1.4 Map"></a>1.4 Map</h3><p>es6 以前，在 js 中实现键值存储可以使用 Object 来方便高效地完成，也就是使用对象属性作为键，在使用属性来引用值。</p>
<p>Map 是一种新的集合类型，为这门语言带来真正的键/值存储机制。Map 的大多数特性都可以通过 Object 类型实现。</p>
<h4 id="1-4-1-基本-API"><a href="#1-4-1-基本-API" class="headerlink" title="1.4.1 基本 API"></a>1.4.1 基本 API</h4><p>使用 new 关键字和 Map 构造函数可以创建一个空映射</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> m = <span class="keyword">new</span> <span class="built_in">Map</span>();</span><br></pre></td></tr></table></figure>

<p>如果想在创建的同时初始化实例，可以给 Map 传递一个可迭代的对象，需要包含键/值对数组。可迭代对象中的每个键/值对都会按照迭代顺序插入到新映射实例中</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> m1 = <span class="keyword">new</span> <span class="built_in">Map</span>([</span><br><span class="line">  [<span class="string">&quot;key1&quot;</span>, <span class="string">&quot;value1&quot;</span>],</span><br><span class="line">  [<span class="string">&quot;key2&quot;</span>, <span class="string">&quot;value2&quot;</span>],</span><br><span class="line">]);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> m2 = <span class="keyword">new</span> <span class="built_in">Map</span>(&#123;</span><br><span class="line">  [<span class="built_in">Symbol</span>.iterator]: <span class="function"><span class="keyword">function</span>* (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">yield</span> [<span class="string">&quot;key&quot;</span>, <span class="string">&quot;val&quot;</span>];</span><br><span class="line">    <span class="keyword">yield</span> [<span class="string">&quot;key2&quot;</span>, <span class="string">&quot;val2&quot;</span>];</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">m2.size; <span class="comment">// 3</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> m3 = <span class="keyword">new</span> <span class="built_in">Map</span>([[]]);</span><br><span class="line">m3.has(<span class="literal">undefined</span>); <span class="comment">// true</span></span><br><span class="line">m3.get(<span class="literal">undefined</span>); <span class="comment">// undefined</span></span><br></pre></td></tr></table></figure>

<p>初始化之后可以使用 set()方法在添加键值对。可以使用 get()和 has()进行查询，可以通过 size 类型属性获取映射中的键/值对的数量，还可以使用 delete()和 clear()删除值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> m = <span class="keyword">new</span> <span class="built_in">Map</span>();</span><br><span class="line">m.set(<span class="string">&quot;firstname&quot;</span>, <span class="string">&quot;mat&quot;</span>);</span><br><span class="line">m.has(<span class="string">&quot;firstname&quot;</span>); <span class="comment">// true</span></span><br><span class="line">m.get(<span class="string">&quot;firstname&quot;</span>); <span class="comment">// mat</span></span><br></pre></td></tr></table></figure>

<p>Set()方法返回映射实例，因此可以把多个操作连缀起来，包括初始声明</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> m = <span class="keyword">new</span> <span class="built_in">Map</span>().set(<span class="string">&quot;key&quot;</span>, <span class="string">&quot;val&quot;</span>);</span><br><span class="line">m.set(<span class="string">&quot;key1&quot;</span>, <span class="string">&quot;val2&quot;</span>);</span><br></pre></td></tr></table></figure>

<p>与 Object 只能使用数组、字符串或符号作为键不同，Map 可以使用任何 js 数据类型作为键</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> m = <span class="keyword">new</span> <span class="built_in">Map</span>();</span><br><span class="line"><span class="keyword">const</span> functionKey = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;&#125;;</span><br><span class="line"><span class="keyword">const</span> symbolKey = <span class="built_in">Symbol</span>();</span><br><span class="line"><span class="keyword">const</span> objectKey = <span class="keyword">new</span> <span class="built_in">Object</span>();</span><br><span class="line">m.set(functionKey, <span class="string">&quot;functionKey&quot;</span>);</span><br><span class="line">m.set(symbolKey, <span class="string">&quot;symbolKey&quot;</span>);</span><br><span class="line">m.get(functionKey);</span><br></pre></td></tr></table></figure>

<p>与严格相等一样，在映射中用作键和值对象及其他“集合”类型，在自己的内容或属性被修改时仍然保持不变</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> m = <span class="keyword">new</span> <span class="built_in">Map</span>();</span><br><span class="line"><span class="keyword">const</span> objKey = &#123;&#125;,</span><br><span class="line">  objVal = &#123;&#125;,</span><br><span class="line">  arrKey = [],</span><br><span class="line">  arrVal = [];</span><br><span class="line">m.set(objKey, objVal);</span><br><span class="line">m.set(arrKey, arrVal);</span><br><span class="line">objKey.foo = <span class="string">&quot;foo&quot;</span>;</span><br><span class="line">objVal.bar = <span class="string">&quot;bar&quot;</span>;</span><br><span class="line">arrKey.push(<span class="string">&quot;foo&quot;</span>);</span><br><span class="line">arrValue.push(<span class="string">&quot;bar&quot;</span>);</span><br><span class="line">m.get(objKey); <span class="comment">// &#123;bar:&#x27;bar&#x27;&#125;</span></span><br><span class="line">m.get(arrKey); <span class="comment">// [&#x27;bar&#x27;]</span></span><br></pre></td></tr></table></figure>

<h4 id="1-4-2-顺序与迭代"><a href="#1-4-2-顺序与迭代" class="headerlink" title="1.4.2 顺序与迭代"></a>1.4.2 顺序与迭代</h4><p>与 Object 类型的一个主要差异是，Map 实例会维护键值对的插入顺序，因此可以根据插入顺序执行迭代操作</p>
<p>映射实例可以提供一个迭代器（iterator)，能以插入顺序生成[key,value]形式的数组。通过 entries 的方法或者 Symbol.iterator 属性，它引用 entries()取得这个迭代器：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> m = <span class="keyword">new</span> <span class="built_in">Map</span>().set(<span class="string">&quot;key&quot;</span>, <span class="string">&quot;val&quot;</span>);</span><br><span class="line">m.set(<span class="string">&quot;key1&quot;</span>, <span class="string">&quot;val2&quot;</span>);</span><br><span class="line">m.entries == m[<span class="built_in">Symbol</span>.iterator]; <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<p>因为 entries 是默认迭代器，所以可以直接对映射实例使用扩展操作，把映射转换为数组</p>
<h4 id="1-4-3-选择-Object-还是-Map"><a href="#1-4-3-选择-Object-还是-Map" class="headerlink" title="1.4.3 选择 Object 还是 Map"></a>1.4.3 选择 Object 还是 Map</h4><p>对象和映射之间存在显著的差别</p>
<ul>
<li>内存占用</li>
</ul>
<p>Object 和 Map 工程级实现在不同浏览器间存在明显差异，但存储单个键/值对所占用的内存数量都会随键的数量线性增加。批量添加或删除键/值则取决于各浏览器对该类型内存分配的工程实现，不同浏览器的情况不同，但给定固定大小的内存，Map 大约可以比 Object 多存储 50%的键/值对</p>
<ul>
<li>插入性能</li>
</ul>
<p>如果代码设计大量插入操作，Map 性能更佳</p>
<ul>
<li>查找速度</li>
</ul>
<p>如果代码设计大量的查找操作，选择 Object 更好一些</p>
<ul>
<li>删除性能</li>
</ul>
<p>使用 delete 删除 object 属性一直以来饱受诟病。目前浏览器出现一些伪删除对象属性的操作，包括把属性值设置为 undefined 或 null。而对大量的引擎来说，Map 的 delete 操作都比插入和查找更快。如果代码涉及到大量删除操作，应该选择 Map</p>
<h3 id="1-5-WeekMap"><a href="#1-5-WeekMap" class="headerlink" title="1.5 WeekMap"></a>1.5 WeekMap</h3><p>es6 新增的弱映射是一种新的集合类型，为了这门语言带来增强的键/值对存储机制。WeekMap 中的 week,描述的就是 js 垃圾回收程序对待弱映射中键的方式</p>
<h4 id="1-5-1-基本-API"><a href="#1-5-1-基本-API" class="headerlink" title="1.5.1 基本 API"></a>1.5.1 基本 API</h4><p>可以通过 new 关键字实例化一个 weekmap</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> wm = <span class="keyword">new</span> <span class="built_in">WeakMap</span>();</span><br></pre></td></tr></table></figure>

<ul>
<li>弱映射中的键只能是 Object 或者继承自 Object 的类型，尝试使用非对象设置键会抛出 TypeError</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> map = <span class="keyword">new</span> <span class="built_in">WeakMap</span>();</span><br><span class="line">map.set(<span class="number">1</span>, <span class="number">2</span>); <span class="comment">// TypeError: Invalid value used as weak map key</span></span><br><span class="line">map.set(<span class="literal">null</span>, <span class="number">2</span>); <span class="comment">// TypeError: Invalid value used as weak map key</span></span><br></pre></td></tr></table></figure>

<ul>
<li>WeakMap 的键名所引用的对象是弱引用</li>
</ul>
<p>WeakMaps 保持了对建名所引用的对象的弱引用</p>
<p>弱引用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">在计算机程序设计中，弱引用和强引用相对，是指不能确保其引用的对象不会被垃圾回收器回收的引用。一个对象若只被弱引用所引用，则被认为是不可访问的，并因此可能在任何时候回收</span><br></pre></td></tr></table></figure>

    </div>

    
    
    
      <div>
          
            
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <p><span>本文标题:</span><a href="/codeblog/2021/02/27/%E9%9B%86%E5%90%88%E5%BC%95%E7%94%A8%E7%B1%BB/">Javascript基础（五）：javascript集合引用类型</a></p>
  <p><span>文章作者:</span><a href="/" title="访问  的个人博客"></a></p>
  <p><span>发布时间:</span>2021年02月27日 - 10:02</p>
  <p><span>最后更新:</span>2021年04月15日 - 09:04</p>
  <p><span>原始链接:</span><a href="/codeblog/2021/02/27/%E9%9B%86%E5%90%88%E5%BC%95%E7%94%A8%E7%B1%BB/" title="Javascript基础（五）：javascript集合引用类型">http://skycoin.gitee.io/codeblog/2021/02/27/%E9%9B%86%E5%90%88%E5%BC%95%E7%94%A8%E7%B1%BB/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://skycoin.gitee.io/codeblog/2021/02/27/%E9%9B%86%E5%90%88%E5%BC%95%E7%94%A8%E7%B1%BB/"  aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span><i class="fa fa-creative-commons"></i> 转载请保留原文链接及作者。</p>  
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
    $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
    });
    });  
</script>



          
    </div>
        <div class="reward-container">
  <div>Donate comment here.</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/codeblog/images/wechatpay.png" alt="若说 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/codeblog/images/alipay.png" alt="若说 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/images/wechat_channel.jpg">
            <span class="icon">
              <i class="fab fa-weixin"></i>
            </span>

            <span class="label">WeChat</span>
          </a>
        </div>

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/codeblog/tags/%E5%89%8D%E7%AB%AF/" rel="tag"># 前端</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item"></div>
      <div class="post-nav-item">
    <a href="/codeblog/2021/02/27/%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5/" rel="next" title="Javascript基础（二）：javascript基本概念">
      Javascript基础（二）：javascript基本概念 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%AF%B9%E8%B1%A1"><span class="nav-number">1.</span> <span class="nav-text">1.对象</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-Object"><span class="nav-number">1.1.</span> <span class="nav-text">1.1 Object</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-Array"><span class="nav-number">1.2.</span> <span class="nav-text">1.2 Array</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-1-%E5%88%9B%E5%BB%BA%E6%95%B0%E7%BB%84"><span class="nav-number">1.2.1.</span> <span class="nav-text">1.2.1 创建数组</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-2-%E6%95%B0%E7%BB%84%E7%A9%BA%E4%BD%8D"><span class="nav-number">1.2.2.</span> <span class="nav-text">1.2.2 数组空位</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-3-%E6%95%B0%E7%BB%84%E7%B4%A2%E5%BC%95"><span class="nav-number">1.2.3.</span> <span class="nav-text">1.2.3 数组索引</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-4-%E6%A3%80%E6%B5%8B%E6%95%B0%E7%BB%84"><span class="nav-number">1.2.4.</span> <span class="nav-text">1.2.4 检测数组</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-5-%E8%BF%AD%E4%BB%A3%E5%99%A8%E6%96%B9%E6%B3%95"><span class="nav-number">1.2.5.</span> <span class="nav-text">1.2.5 迭代器方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-6-%E5%A4%8D%E5%88%B6%E5%92%8C%E5%A1%AB%E5%85%85%E6%96%B9%E6%B3%95"><span class="nav-number">1.2.6.</span> <span class="nav-text">1.2.6 复制和填充方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-7-%E8%BD%AC%E6%8D%A2%E6%96%B9%E6%B3%95"><span class="nav-number">1.2.7.</span> <span class="nav-text">1.2.7 转换方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-8-%E6%A0%88%E6%96%B9%E6%B3%95"><span class="nav-number">1.2.8.</span> <span class="nav-text">1.2.8 栈方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-9-%E9%98%9F%E5%88%97%E6%96%B9%E6%B3%95"><span class="nav-number">1.2.9.</span> <span class="nav-text">1.2.9 队列方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-10-%E6%8E%92%E5%BA%8F%E6%96%B9%E6%B3%95"><span class="nav-number">1.2.10.</span> <span class="nav-text">1.2.10 排序方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-11-%E6%93%8D%E4%BD%9C%E6%96%B9%E6%B3%95"><span class="nav-number">1.2.11.</span> <span class="nav-text">1.2.11 操作方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-12-%E6%90%9C%E7%B4%A2%E5%92%8C%E4%BD%8D%E7%BD%AE%E6%96%B9%E6%B3%95"><span class="nav-number">1.2.12.</span> <span class="nav-text">1.2.12 搜索和位置方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-13-%E8%BF%AD%E4%BB%A3%E6%96%B9%E6%B3%95"><span class="nav-number">1.2.13.</span> <span class="nav-text">1.2.13 迭代方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-14-%E5%BD%92%E5%B9%B6%E6%96%B9%E6%B3%95"><span class="nav-number">1.2.14.</span> <span class="nav-text">1.2.14 归并方法</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E5%AE%9A%E5%9E%8B%E6%95%B0%E7%BB%84"><span class="nav-number">1.3.</span> <span class="nav-text">1.3 定型数组</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-1-%E5%8E%86%E5%8F%B2"><span class="nav-number">1.3.1.</span> <span class="nav-text">1.3.1 历史</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-2-ArrayBuffer"><span class="nav-number">1.3.2.</span> <span class="nav-text">1.3.2 ArrayBuffer</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-3-DateView"><span class="nav-number">1.3.3.</span> <span class="nav-text">1.3.3 DateView</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-4-%E5%AE%9A%E5%9E%8B%E6%95%B0%E7%BB%84"><span class="nav-number">1.3.4.</span> <span class="nav-text">1.3.4 定型数组</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-Map"><span class="nav-number">1.4.</span> <span class="nav-text">1.4 Map</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-4-1-%E5%9F%BA%E6%9C%AC-API"><span class="nav-number">1.4.1.</span> <span class="nav-text">1.4.1 基本 API</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-4-2-%E9%A1%BA%E5%BA%8F%E4%B8%8E%E8%BF%AD%E4%BB%A3"><span class="nav-number">1.4.2.</span> <span class="nav-text">1.4.2 顺序与迭代</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-4-3-%E9%80%89%E6%8B%A9-Object-%E8%BF%98%E6%98%AF-Map"><span class="nav-number">1.4.3.</span> <span class="nav-text">1.4.3 选择 Object 还是 Map</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-5-WeekMap"><span class="nav-number">1.5.</span> <span class="nav-text">1.5 WeekMap</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-5-1-%E5%9F%BA%E6%9C%AC-API"><span class="nav-number">1.5.1.</span> <span class="nav-text">1.5.1 基本 API</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="若说"
      src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=4083356047,3551369852&fm=26&gp=0.jpg">
  <p class="site-author-name" itemprop="name">若说</p>
  <div class="site-description" itemprop="description">立志完成前端知识体系的架构</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/codeblog/archives/">
        
          <span class="site-state-item-count">5</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/codeblog/categories/">
          
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/lovebxy" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;lovebxy" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/codeblog/1490365616@qq.com" title="E-Mail → 1490365616@qq.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/yourname" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://plus.google.com/yourname" title="Google → https:&#x2F;&#x2F;plus.google.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fab fa-google fa-fw"></i>Google</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://yoursite.com/" title="http:&#x2F;&#x2F;yoursite.com" rel="noopener" target="_blank">test</a>
        </li>
    </ul>
  </div>

      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">若说</span>
</div>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共25.5k字</span>
</div>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="powered-by">
  <i class="fa fa-user-md"></i>
  <span id="busuanzi_container_site_pv">
    本站访问量:<span id="busuanzi_value_site_pv"></span>次
  </span>
  <span class="post-meta-divider">|</span>
  <span id="busuanzi_container_site_uv">
    本站总访客量：<span id="busuanzi_value_site_uv"></span>人
  </span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/codeblog/lib/anime.min.js"></script>
  <script src="/codeblog/lib/velocity/velocity.min.js"></script>
  <script src="/codeblog/lib/velocity/velocity.ui.min.js"></script>

<script src="/codeblog/js/utils.js"></script>

<script src="/codeblog/js/motion.js"></script>


<script src="/codeblog/js/schemes/pisces.js"></script>


<script src="/codeblog/js/next-boot.js"></script>




  




  
<script src="/codeblog/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : true,
      notify     : false,
      appId      : 'DbTfuJfplEWGguVvLyabTu99-gzGzoHsz',
      appKey     : 'hkLIIwmXwOd95taiLRxnUrjO',
      placeholder: "Just go go",
      avatar     : 'monsterid',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

  <script type="text/javascript" src="/js/src/clicklove.js"></script>
  <script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>

</body>
</html>
